package com.ylbh.modules.sign.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.sign.dto.SignDailyTasksDTO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.sign.entity.ShoppingSigninDailyTask;
import com.ylbh.modules.sign.mapper.ShoppingDailyTasksMapper;
import com.ylbh.modules.sign.service.ShoppingDailyTasksService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author wjc
 */
@Service
public class ShoppingDailyTasksServiceImpl implements ShoppingDailyTasksService {

    @Autowired
    private ShoppingDailyTasksMapper shoppingDailyTasksMapper;
    @Override
    public PageUtils queryDailyTasksList(SignDailyTasksDTO signDailyTasksDTO) {
        Page<Object> page = new Page<>(signDailyTasksDTO.getPage(),signDailyTasksDTO.getLimit());
        List<ShoppingSigninDailyTask> DailyTask = shoppingDailyTasksMapper.selectDailyTasksList(signDailyTasksDTO, page);
        Page<ShoppingSigninDailyTask> page1 = new Page<>();
        page1.setRecords(DailyTask).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    @Override
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        OSSClientUtil ossClient = null;
        String path = request.getParameter("path");
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,path);//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/" + path + "/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public void addDailyTasks(SignDailyTasksDTO signDailyTasksDTO) {
        ShoppingSigninDailyTask dTask = new ShoppingSigninDailyTask();
        BeanUtils.copyProperties(signDailyTasksDTO,dTask);
        IdWorker idW = new IdWorker();
        Date date = new Date();
        dTask.setCreateTime(date);
        dTask.setCreateUserId(ShiroUtils.getUserId().toString());
        dTask.setUpdateTime(date);
        dTask.setUpdateUserId(ShiroUtils.getUserId().toString());
        dTask.setId(idW.nextId());
        shoppingDailyTasksMapper.insertDailyTasks(dTask);
    }

    @Override
    public ShoppingSigninDailyTask getDailyTasksById(String id) {
        return shoppingDailyTasksMapper.selectDailyTasksById(id);
    }

    @Override
    public void modifyTurntableById(SignDailyTasksDTO signDailyTasksDTO) {
        ShoppingSigninDailyTask dTask = new ShoppingSigninDailyTask();
        BeanUtils.copyProperties(signDailyTasksDTO,dTask);
        Date date = new Date();
        dTask.setUpdateTime(date);
        dTask.setUpdateUserId(ShiroUtils.getUserId().toString());
        shoppingDailyTasksMapper.updateDailyTasks(dTask);
    }

    @Override
    public void removeDailyTasksById(String id) {
        shoppingDailyTasksMapper.updateDailyTasksById(id);
    }

    @Override
    public void modifyTaskEnableStateById(SignDailyTasksDTO signDailyTasksDTO) {
        shoppingDailyTasksMapper.updateTaskEnableState(signDailyTasksDTO);
    }
}
